package com.persion.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.persion.common.annotation.Anonymous;
import com.persion.common.config.RuoYiConfig;
import com.persion.common.constant.Constants;
import com.persion.common.core.domain.AjaxResult;
import com.persion.common.utils.StringUtils;
import com.persion.common.utils.file.FileUtils;
import com.persion.domain.Activity;
import com.persion.domain.Service;
import com.persion.dto.Params;
import com.persion.service.ServiceService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.persion.vo.ServiceVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

/**
 * 养老服务列表(Service)表控制层
 *
 * @author WangFK
 * @since 2024-01-27 14:38:58
 */
@RestController
@Slf4j
@RequestMapping("/service")
public class ServiceController {
    /**
     * 服务对象
     */
    @Resource
    private ServiceService serviceService;

    /**
     * 分页查询
     * @Author WangFK
     * @Date 2024/1/27 14:52
     * @Param [params]
     * @return com.persion.common.core.domain.AjaxResult
     **/
    @Anonymous
    @GetMapping("/list")
    public AjaxResult list(Params params){
        String search = params.getSearch();
        Page<Service> page = new Page<>(params.pageNum, params.pageSize);
        LambdaUpdateWrapper<Service> lqw = new LambdaUpdateWrapper<Service>()
                .like(StringUtils.isNotEmpty(search), Service::getServiceTitle, search).or()
                .like(StringUtils.isNotEmpty(search), Service::getServiceIntro, search).or()
                .like(StringUtils.isNotEmpty(search), Service::getServiceCharge, search)
                .eq(ObjectUtil.isNotEmpty(params.getActStatus()), Service::getActStatus, params.getActStatus())
                .eq(ObjectUtil.isNotEmpty(params.getCode()), Service::getAreaCode, params.getCode());
        if (ObjectUtil.isEmpty(params.getFlag())){
            lqw.eq(Service::getActStatus,"0");
        }
        List<Service> records = serviceService.page(page, lqw).getRecords();
        if (ObjectUtil.isEmpty(records)){
            return AjaxResult.warn("暂无信息");
        }
        List<ServiceVo> serviceVos = BeanUtil.copyToList(records, ServiceVo.class);
        serviceVos.stream().forEach(item->{
            item.setActPlaceDetail(item.getProvinceName() + item.getCityName() + item.getDistrictName() + item.getServicePlace());
        });
        return AjaxResult.success().put("rows",serviceVos).put("total",serviceVos.size());
    }
    /**
     * 上下架分类状态
     *
     * @param service
     * @return
     */
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Service service) {
        LambdaUpdateWrapper<Service> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(Service::getServiceId, service.getServiceId())
                .set(Service::getActStatus, service.getActStatus());
        return serviceService.update(lqw) ? AjaxResult.success() : AjaxResult.warn("修改状态失败");
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return AjaxResult.success(serviceService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param service 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody Service service) {
        Date actBtime = service.getActBtime();
        Date actEtime = service.getActEtime();
        if (DateUtil.compare(DateUtil.yesterday(), actBtime) > 0) {
            return AjaxResult.warn("不能早于今天：" + DateUtil.now());
        }
        if (DateUtil.compare(actBtime, actEtime) > 0) {
            return AjaxResult.warn("开始时间：" + DateUtil.format(actBtime, "yyyy-MM-dd") + "不能比结束时间：" + DateUtil.format(actEtime, "yyyy-MM-dd") + "晚");
        }
        if (StringUtils.isEmpty(service.getServiceTitle()) || StringUtils.isEmpty(service.getServiceIntro()) ||
                StringUtils.isEmpty(service.getActPhoto()) || StringUtils.isEmpty(service.getAreaCode()) ||
                StringUtils.isEmpty(service.getServicePlace()) || StringUtils.isEmpty(service.getServiceCharge()) ||
                StringUtils.isEmpty(actBtime.toString()) || StringUtils.isEmpty(actEtime.toString())) {
            return AjaxResult.warn("请按规范填写!");
        }
        if (ObjectUtil.isNotEmpty(service.getServiceId())) {
            return update(service);
        }
        return AjaxResult.success(serviceService.save(service));
    }

    /**
     * 修改数据
     *
     * @param service 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody Service service) {
        Service byId = serviceService.getById(service.getServiceId());
        if (!service.getActPhoto().equalsIgnoreCase(byId.getActPhoto())){
            // 本机地址
            String localPath = RuoYiConfig.getProfile();
            String downloadPath = localPath + StringUtils.substringAfter(byId.getActPhoto(), Constants.RESOURCE_PREFIX);
            FileUtils.deleteFile(downloadPath);
        }
        return AjaxResult.success(serviceService.updateById(service));
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Serializable id) {
        return AjaxResult.success(serviceService.removeById(id));
    }
}

